package com.example.yichuangyun.utils;

import android.app.Activity;

import com.example.yichuangyun.ui.activity.MainActivity;

import java.util.Stack;

/**
 * 视图切换处理栈
 */
public class ActivityStackManager {
    private Stack<Activity> activityStack;
    private static ActivityStackManager instance;

    private ActivityStackManager() {
    }

    public static ActivityStackManager getInstance() {
        if (instance == null) {
            instance = new ActivityStackManager();
        }
        return instance;
    }

    public static void releaseInstance() {
        if (instance != null) {
            instance.exitActivity();
            if (instance.activityStack != null) {
                instance.activityStack.clear();
                instance.activityStack = null;
            }
            instance = null;
        }
    }

    /**
     * 退出到某一个栈的位置
     */
    public void popToActivity(Class<?> cls, boolean popThis) {
        if (activityStack != null && !activityStack.isEmpty()) {
            int startIndex = -1;
            for (int i = activityStack.size() - 1; i >= 0; i--) {
                if (activityStack.get(i).getClass().equals(cls)) {
                    startIndex = i;
                    break;
                }
            }
            if (startIndex != -1) {
                for (int i = activityStack.size() - 1; i >= 0; i--) {
                    if (activityStack.get(i).getClass().equals(cls)) {
                        if (popThis) {
                            if (!activityStack.get(i).isFinishing()) {
                                activityStack.get(i).finish();
                            }
                            activityStack.remove(i);
                        }
                        break;
                    } else {
                        if (!activityStack.get(i).isFinishing()) {
                            activityStack.get(i).finish();
                        }
                        activityStack.remove(i);
                        i = activityStack.size();// 此处为什么要给i赋值？？？？
                    }
                }
            }
        }
    }

    /**
     * 获取当前Activity 是否为栈低
     */
    public boolean isTaskRoot() {
        if (activityStack != null && !activityStack.isEmpty()) {
            return activityStack.size() <= 1;
        }
        return true;
    }

    /**
     * 移除栈顶并获取第二的Activity
     */
    public Activity removeActivityTop() {
        if (activityStack != null && !activityStack.isEmpty()) {
            return activityStack.pop();
        }
        return null;
    }

    /**
     * 退出栈顶Activity
     */
    public void popActivity(Activity activity) {
        if (activity != null) {
            // 在从自定义集合中取出当前Activity时，也进行了Activity的关闭操作
            if (!activity.isFinishing()) {
                activity.finish();
            }

            if (activityStack != null) {
                activityStack.remove(activity);
            }
            activity = null;
        }
    }

    /**
     * 将当前Activity推入栈中
     */
    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.push(activity);
    }

    /**
     * 获得当前栈顶Activity(从最早放入的Activity开始返回)
     */
    public Activity firstActivity() {
        Activity activity = null;
        if (activityStack == null) {
            return null;
        }
        if (!activityStack.empty()) {
            activity = activityStack.firstElement();
        }
        return activity;
    }

    /**
     * 获得当前栈顶Activity
     */
    public Activity currentActivity() {
        Activity activity = null;
        if (activityStack == null) {
            return null;
        }

        if (!activityStack.empty()) {
            activity = activityStack.lastElement();
        }
        return activity;
    }

    /**
     * 获得当前栈底Activity(homeActivity)
     */
    public Activity getHomeActivity() {
        Activity activity = null;
        if (activityStack == null) {
            return null;
        }

        if (!activityStack.empty()) {
            activity = activityStack.firstElement();
        }

        return activity;
    }


    /**
     * 移除但不关闭
     */
    public void removeActivityNoFinish(Activity activity) {
        if (activity != null && activityStack != null) {
            activityStack.remove(activity);
        }
    }

    /**
     * 退出栈中所有Activity
     */
    public void popAllActivityExceptOne(Activity tempActivity) {
        if (activityStack != null && !activityStack.isEmpty()) {
            for (int i = activityStack.size() - 1; i >= 0; i--) {
                Activity activity = activityStack.get(i);
                if (activity == null) {
                    continue;
                }

                if (activity == tempActivity) {
                    // activityStack.remove(activity);
                } else {
                    if (!activity.isFinishing()) {
                        activity.finish();
                    }
                    activityStack.remove(activity);
                }
            }
        }
    }

    /**
     * 退出栈中所有Activity
     */
    public void popAllActivityExceptMain() {
        if (activityStack != null && !activityStack.isEmpty()) {
            for (int i = activityStack.size() - 1; i >= 0; i--) {
                Activity activity = activityStack.get(i);
                if (activity == null) {
                    continue;
                }

                if (activity instanceof MainActivity) {
                    // activityStack.remove(activity);
                } else {
                    if (!activity.isFinishing()) {
                        activity.finish();
                    }
                    activityStack.remove(activity);
                }
            }
        }
    }

    /**
     * 移除和关闭指定CLASS的Activity
     */
    public void popActivityOfClass(Class<?> cls) {
        if (activityStack != null && !activityStack.isEmpty()) {
            for (int i = activityStack.size() - 1; i >= 0; i--) {
                if (activityStack.get(i).getClass().equals(cls)) {
                    if (!activityStack.get(i).isFinishing()) {
                        activityStack.get(i).finish();
                    }
                    activityStack.remove(i);
                    break;
                }
            }
        }
    }

    /**
     * 退出应用程序
     */
    public void exitActivity() {
        while (true) {
            Activity activity = firstActivity();
            if (activity == null) {
                break;
            }
            popActivity(activity);
        }
    }
//
//    private void printActivityStack() {
////		System.out.println("**********************printActivityStack start");
//        if (activityStack != null && !activityStack.isEmpty()) {
//            for (int i = activityStack.size() - 1; i >= 0; i--) {
////				System.out.println("***********************print:"
////						+ activityStack.get(i));
//            }
//        } else {
////			System.out.println("*****************empty");
//        }
//    }


}
